global
    maxconn 100000
    daemon
    ca-base /srv/newsblur/config/certificates
    crt-base /srv/newsblur/config/certificates
    tune.bufsize 32000
    tune.maxrewrite 8196
    tune.ssl.default-dh-param 2048
    ssl-default-bind-options no-sslv3 no-tlsv10 no-tlsv11 no-tls-tickets
    ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
    log 127.0.0.1 local0 notice
    # log 127.0.0.1 local1 info
    stats socket /var/run/haproxy.sock mode 660 level admin

resolvers consul
    nameserver consul 127.0.0.1:53
    accepted_payload_size 8192 # allow larger DNS payloads

defaults
    log global
    maxconn 100000
    mode http
    option forwardfor
    option http-server-close
    option httpclose
    option log-health-checks
    option log-separate-errors
    option httplog
    option redispatch
    option abortonclose
    timeout connect 10s
    timeout client 10s
    timeout server 120s
    timeout tunnel 1h
    retries 2
    errorfile 502 /srv/newsblur/templates/502.http
    errorfile 503 /srv/newsblur/templates/502.http
    errorfile 504 /srv/newsblur/templates/502.http

frontend public
    bind :80
    bind :443 ssl crt /srv/newsblur/config/certificates/newsblur.com.pem alpn h2,http/1.1
    http-response add-header Strict-Transport-Security max-age=0;\ includeSubDomains
    option http-server-close

    acl gunicorn_dead nbsrv(app_django) lt 1
    acl nginx_dead nbsrv(nginx) lt 1
    acl mx_mode nbsrv(maintenance) lt 1
    acl is_unread_count url_beg /reader/feed_unread_count
    acl is_refresh_feeds url_beg /reader/refresh_feed
    acl is_original_text url_beg /rss_feeds/original_text
    acl is_river url_beg /reader/river_stories
    acl is_automated_river urlp(h) -m found
    acl is_dashboard url_param(dashboard) -i true

    monitor-uri /status
    monitor fail if gunicorn_dead
    monitor fail if nginx_dead
    monitor fail if mx_mode

    # Redirect all HTTP traffic to HTTPS
    acl is_root path /
    redirect scheme https if is_root !{ ssl_fc }
    
    use_backend node_socket if { path_beg /v3/socket.io/ }
    use_backend node_favicon if { path_beg /rss_feeds/icon/ }
    use_backend node_text if { path_beg /rss_feeds/original_text_fetcher }
    use_backend node_page if { path_beg /original_page/ }
    use_backend nginx if { path_beg /media/ }
    use_backend db_metrics if { hdr_end(host) -i metrics.newsblur.com }
    use_backend nginx if { path_beg /static/ }
    use_backend nginx if { path_beg /favicon }
    use_backend nginx if { path_beg /crossdomain/ }
    use_backend nginx if { path_beg /robots }
    #use_backend self if { path_beg /munin/ }

    use_backend nginx if mx_mode

    use_backend app_django_counts if is_unread_count
    use_backend app_django_refresh if is_refresh_feeds
    use_backend app_django_refresh if is_dashboard
    use_backend app_django_refresh if is_original_text
    use_backend app_django_refresh if is_river is_automated_river
    use_backend app_django unless gunicorn_dead || nginx_dead


backend node_socket
    balance roundrobin
    server node_socket 127.0.0.1:8008 check inter 3000ms

backend node_favicon
    http-check expect rstatus 200|301|503
    option httpchk GET /rss_feeds/icon/1
    balance roundrobin
    server node_favicon 127.0.0.1:8008 check inter 3000ms

backend node_text
    http-check expect rstatus 200|503
    option httpchk GET /rss_feeds/original_text_fetcher?test=1
    balance roundrobin
    server node_text 127.0.0.1:8008 check inter 3000ms

backend node_page
    http-check expect rstatus 200|503
    option httpchk GET /original_page/1?test=1
    balance roundrobin
    server node_page 127.0.0.1:8008 check inter 3000ms

backend db_metrics
    # option httpchk GET /_haproxychk
    server staging-grafana 127.0.0.1:3000 check inter 3000ms

backend nginx
    balance roundrobin
    option httpchk GET /_nginxchk
    http-check expect rstatus 200|503
    server nginx 127.0.0.1:81 check inter 60000ms

backend app_django_counts
    balance roundrobin
    option httpchk GET /_haproxychk
    retry-on all-retryable-errors
    server hstaging 127.0.0.1:8000 check inter 1000ms fall 2

backend app_django_refresh
    balance roundrobin
    option httpchk GET /_haproxychk
    retry-on all-retryable-errors
    server hstaging 127.0.0.1:8000 check inter 1000ms fall 2

backend app_django
    balance roundrobin
    option httpchk GET /_haproxychk
    retry-on all-retryable-errors
    server hstaging 127.0.0.1:8000 check inter 1000ms fall 2

backend maintenance
    option httpchk HEAD /maintenance
    http-check expect status 404
    http-check send-state
    server nginx 127.0.0.1:81 check inter 60000ms

listen stats
bind :1936 ssl crt /srv/newsblur/config/certificates/newsblur.com.pem ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES128-SHA:AES256-SHA256:AES256-SHA no-sslv3
stats enable
stats hide-version
stats realm Haproxy\ Statistics
stats uri /
stats auth gimmiestats:StatsGiver
stats refresh 15s
